<?php
require_once ('../configs/yikuaiqu_config.php');
require_once ('../functions/base.php');
require_once ('../functions/functions.user.php');
require_once ('../functions/functions.yikuaiqu.php');
require_once ('../configs/message.config.php');

$json_arr = array();
if(!check_valid_user()){
	$json_arr['ret_code'] = ERROR_NO_NOT_LOGIN;
	$json_arr['ret_message'] = ERROR_NOT_LOGIN;
}else{
	$comment_content = 0 + $_REQUEST["comment_content"];
	$userModel = get_user_info();
	$comment_user = $userModel['user_id'];
	$comment_status = 1;
	if($comment_content > 0){ 
		if($comment_user > 0){
			$sql = "INSERT INTO tysvc.tbComment(fdUserID,fdContentID,fdCreate,fdType,fdParentID) "
					." SELECT ?,?,now(),2,0 "
					."FROM dual WHERE NOT EXISTS (SELECT * FROM tysvc.tbComment WHERE fdParentID = 0 AND fdUserID = ? AND fdContentID = ? AND fdType = 2)";
			$mysqli = db_connect();
			$stmt = $mysqli->stmt_init();
			$stmt->prepare($sql)or die("prepare get sence sql error22.");
			$stmt->bind_param("iiii", $comment_user, $comment_content, $comment_user, $comment_content);
			$stmt->execute();
			$stmt->fetch();
			
			if($stmt->affected_rows == 0){
				$sql = "DELETE FROM tysvc.tbComment WHERE fdUserID = ? AND fdParentID = 0 AND fdContentID = ? AND fdType = 2";
				$stmt->free_result();
				$stmt->prepare($sql);
				$stmt->bind_param("ii", $comment_user,$comment_content);
				$stmt->execute();
				
				$sql = "UPDATE tyctn.tbInteger SET fdValue = fdValue - 1 WHERE fdContentID = ?  AND fdAttributeID = 51 AND fdValue > 0";
				$stmt->free_result();
				$stmt->prepare($sql)or die("prepare get sence sql error4.");
				$stmt->bind_param("i",$comment_content);
				$stmt->execute();
				$comment_status = 0;

			}else{
				$sql = "UPDATE tyctn.tbInteger set fdValue = fdValue + 1 WHERE fdContentID = ?  AND fdAttributeID = 51";
				$stmt->free_result();
				$stmt->prepare($sql)or die("prepare get sence sql error3.");
				$stmt->bind_param("i",$comment_content);
				$stmt->execute();
				$stmt->fetch();
				if($stmt->affected_rows == 0){
					$sql = "INSERT INTO tyctn.tbInteger (fdContentID, fdAttributeID, fdValue) SELECT ?, 51, 1 "
						."FROM dual WHERE NOT EXISTS (SELECT * FROM tyctn.tbInteger WHERE fdContentID = ? AND fdAttributeID = 51 )";
					$stmt->free_result();
					$stmt->prepare($sql)or die("prepare get sence sql error1.");
					$stmt->bind_param("ii",$comment_content,$comment_content);
					$stmt->execute();
				}
			}
			
			$stmt->free_result();
			$sql = "SELECT SUM(fdValue) FROM tyctn.tbInteger WHERE tbInteger.fdContentID = ? AND tbInteger.fdAttributeID in (50,51)";
			$stmt->prepare($sql)or die("prepare get sence sql error2.");
			$stmt->bind_param("i", $comment_content);
			$stmt->bind_result($ret_like_cnt);
			$stmt->execute();
			$stmt->fetch();

			$json_arr['ret_code'] = 600;
			$json_arr['ret_message'] = "操作成功";
			$json_arr['ret_like_cnt'] = 0 + $ret_like_cnt ;
			$json_arr['ret_like_status'] = $comment_status;
			
			$stmt->close();
			$mysqli->close();
		}else{
			$json_arr['ret_code'] = 604;
			$json_arr['ret_message'] = "非法用户";
		}
	}else{
		$json_arr['ret_code'] = 605;
		$json_arr['ret_message'] = "无效景区";
	}
}
$json = json_encode($json_arr);
echo $json;
?>